package com.free.semantic.core.impl.demo;

import com.free.semantic.common.Ls;
import com.free.semantic.common.ParamNode;
import com.free.semantic.core.ConceptManage;
import com.free.semantic.core.Context;
import com.free.semantic.core.RelationFunction;
import com.free.semantic.core.impl.BaseTest;
import com.free.semantic.core.impl.ConceptInstanceManageImpl;
import com.free.semantic.req.RelationDefineGroup;
import com.free.semantic.utils.Utils;
import org.junit.jupiter.api.Test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 支持单据更新操作测试
 */
public class Demo6_updateScene extends BaseTest {


    @Test
    public void test() {

        RelationDefineGroup group = new RelationDefineGroup();

        defineRelation(group, "属性",
                defineItem(oneObj("A"), oneObj("B")));

        defineRelation(group, "二元找到",
                defineItem(oneObj("A", "B"), oneObj("C")));

        ConceptManage concept = getConceptManage(group);
        concept.addConcept("支付单", "订单金额", "更新");
        Utils.createRelation(concept, "属性", "支付单", "订单金额");
        Utils.createRelation2(concept, "二元找到", "支付单", "订单金额", "更新");


        ConceptInstanceManageImpl conceptMange = new ConceptInstanceManageImpl(concept);

        Map<String, String> payMoneyMap = new HashMap<>();
        payMoneyMap.put("No12345", "123");

        conceptMange.registerFunction(Ls.of("支付单"), "订单金额",
                context -> context.setValue("订单金额",
                        payMoneyMap.get(context.getListValue("支付单").get(0))));


        conceptMange.registerFunction(Ls.of("支付单", "订单金额"), "更新", new RelationFunction() {
            @Override
            public void calculate(Context context) {
                List<String> payNo = context.getListValue("支付单");
                List<String> money = context.getListValue("订单金额");
                payMoneyMap.put(payNo.get(0), money.get(0));
                context.setValue("更新", true);
            }
        });

        List<String> objects = conceptMange.takePath(Ls.of(new ParamNode("支付单", "No12345")), "订单金额");
        assert objects.get(0).equals("123");

        conceptMange.takePath(Ls.of(new ParamNode("支付单", "No12345"),
                new ParamNode("订单金额", "345")), "更新");

        List<String> objects2 = conceptMange.takePath(Ls.of(new ParamNode("支付单", "No12345")), "订单金额");
        assert objects2.get(0).equals("345");


    }


}
